package io.github.lee.core.views.listener

import androidx.recyclerview.widget.RecyclerView

fun RecyclerView.setOnScrollDistanceListener(block: (scrollX: Int, scrollY: Int) -> Unit) {
    addOnScrollListener(object : OnRecyclerViewScrollDistanceListener() {
        override fun onScrollDistance(scrollX: Int, scrollY: Int) {
            block(scrollX, scrollY)
        }
    })
}

/**RecyclerView滚动距离的监听*/
abstract class OnRecyclerViewScrollDistanceListener : RecyclerView.OnScrollListener() {
    private var scrollX = 0
    private var scrollY = 0

    override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
        super.onScrollStateChanged(recyclerView, newState)
    }

    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
        scrollX += dx
        scrollY += dy
        onScrollDistance(scrollX, scrollY)
    }

    abstract fun onScrollDistance(scrollX: Int, scrollY: Int)
}